﻿@{
    ViewData["Title"] = "新闻管理";
    Layout = "~/Views/Shared/_ManageLayout.cshtml";
}
@section css{
    <style>
        .layui-table-cell {
            line-height: 28px;
        }
    </style>
}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">归属语言：</label>
                        <div class="layui-input-inline mr0">
                            <select id="languageId" name="languageId">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchTbBas" lay-submit>
                            <i class="layui-icon">&#xe615;</i>刷新
                        </button>
                        <a class="layui-btn icon-btn" id="addnewsInfo">
                            <i class="layui-icon">&#xe654;</i>发表新闻
                        </a>
                    </div>
                </div>
            </div>

            <table class="layui-table" lay-even lay-skin="line" lay-size="lg" id="tableTbBas" lay-filter="tableTbBas"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- js部分 -->
<script>
    layui.use(['admin', 'layer', 'form', 'table', 'laydate', 'laytpl','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;
        var admin = layui.admin;
        var laytpl = layui.laytpl;
        var upload = layui.upload;
        @Html.LayuiRenderSelect("/api/LanguageType/QueryLanguageSelect", "languageId");
        // 渲染表格
        var insTb = table.render({
            elem: '#tableTbBas',
            url: '/api/newsInfo',
            page: true,
            limit: 30,
            cellMinWidth: 100,
            cols: [[
                { field: 'languageName', align: 'center', title: '语言版本' },
                { field: 'className', align: 'center', title: '所在栏目' },
                { field: 'title', align: 'center', title: '标题' },
                { field: 'newsTime', align: 'center', title: '发表时间' },
                { field: 'sort', align: 'center', title: '排序(越大越靠前)' },
                { templet: '#tableBar', title: '操作', align: 'center', minWidth: 160 }
            ]]
        });

        //监听工具条
        table.on('tool(tableTbBas)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent == "edit") {
                var height = $(window).height() * 0.9;
                var width = $(window).width() * 0.9;
                var check = layer.open({
                    type: 2,
                    title: '新闻编辑',
                    maxmin: true,
                    shadeClose: true, //点击遮罩关闭层
                    area: [width + 'px', height + 'px'],
                    content: '/manage/NewsEdit?Id=' + data.id,
                });
            } else if (layEvent == "del") {
                layer.confirm('确定要删除“[' + data.title + ']”这条新闻吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function (i) {
                    layer.load(2);
                    $.ajax({
                        url: '/api/newsInfo/' + data.id,
                        type: 'delete',
                        async: true,
                        dataType: 'json',
                        complete: function (obj) {
                            layer.closeAll('loading');
                        },
                        success: function (result) {
                            layer.closeAll('loading');
                            if (result.success) {
                                layer.msg(result.message, { icon: 1 });
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(result.message, { icon: 2 });
                            }
                        }
                    });
                });
            }
        });
        // 搜索按钮点击事件
        form.on('submit(formSubSearchTbBas)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } }, 'data');
        });

        //发表新闻
        $("#addnewsInfo").click(function () {
            var height = $(window).height() * 0.9;
            var width = $(window).width() * 0.9;
            var check = layer.open({
                type: 2,
                title: '新闻编辑',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area: [width + 'px', height + 'px'],
                content: '/manage/NewsEdit',
            });
        });
    });
</script>

